<?php
/**
 * 71CMS [ 创先云智慧党建系统 ]
 * =========================================================
 * Copyright (c) 2018-2023 南宁小橙科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.71cms.net
 * 这不是一个自由软件！未经许可不能去掉71CMS相关版权。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 */
namespace app\api\controller;

use app\api\model\Acquaintance;
use app\api\model\CourseComplete;
use app\api\model\Integral;
use app\api\model\TypeToUser;
use app\api\model\UserCourse;
use app\api\model\VideoType;
use think\Db;

class Video extends Base
{
//    public $user;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * 获取分类和分类中的视频列表
     */
    public function get_video(){
        //用户部门
        $user = \app\api\model\User::with(['userDep'=>function($query){
            $query->withJoin(['department'=>function($query){
                $query->where([]);
            }])->where([]);
        }])->get($this->user_id);
        $user['department_info'] = $user['user_dep'][0]['department'];
        //本月学习人数
        $moth_study = $this -> get_moth_study_count();

        //获取当前任务数量
        $mission = $this->complete_count($this->user['id']);

        //分类
        $type = VideoType::where(['status'=>1,'pid'=>0])->order('sort')->select()->toArray();
        foreach($type as $k=>&$item) {
            //课程
            $ctypes = VideoType::where(['pid' => $item['id'], 'status' => 1])->select()->toArray();
            $i = 0;
            foreach ($ctypes as $kk => &$item2) {
                if($i==4){
                    break;
                }
                //查看该课程是否指定
                $is_target = TypeToUser::where(['cid' => $item2['id']])->find();

                if ($is_target) {
                    //如果存在，并且又是指定用户，则加入，否则unset并且continue下一个
                    $check_user = TypeToUser::where(['cid' => $item2['id'], 'uid' => $this->user_id])->find();
                    $check_group = TypeToUser::where(['cid' => $item2['id'], 'gid' => $user['department_info']['id']])->find();

                    if (!$check_user && !$check_group) {
                        unset($ctypes[$kk]);
                        continue;
                    }
                }

                $video = \app\api\model\Video::where(['cid'=>$item2['id']])->order('sort')->select()->toArray();
                if ($video) {
                    $playing = UserCourse::where(['cid' => $item2['id'], 'status' => 0])->count();
                    $video[0]['cname'] = $item2['typename'];
                    $video[0]['count'] = count($video);
                    $video[0]['playing'] = $playing;
                }
                $item2['video'] = $video;
                if(empty($item2['video'])){
                    unset($ctypes[$kk]);
                    continue;
                }
                $i++;

            }
            $item['course'] = $ctypes;

            if(empty($item['course'])){
                unset($type[$k]);
                continue;
            }
        }

        //热门
        $type_to_user = TypeToUser::group('cid')->column('cid');
        $type_to_user = implode(',', $type_to_user);
        $map1 = [['video.cid', 'not in', $type_to_user], ['videoType.status', '=', '1']];

        $type_to_user2 = TypeToUser::where(['uid'=>$this->user_id])->group('cid')->column('cid');
        $type_to_user2 = implode(',', $type_to_user2);
        $map2 = [['video.cid', 'in', $type_to_user2], ['videoType.status', '=', '1']];

        $type_to_user3 = TypeToUser::whereIn('gid',array_column($user->user_dep->toArray(),'id'))->group('cid')->column('cid');
        $type_to_user3 = implode(',', $type_to_user3);
        $map3 = [['video.cid', 'in', $type_to_user3], ['videoType.status', '=', '1']];



        $hot = \app\wap\model\Video::useGlobalScope(false)
            ->field('*,count(video.cid) as count,sum(video.study_count) as study_count')
            ->withJoin(['videoType'=>function($query){
                $query->where([]);
            }])->where(function($query) use($map1,$map2,$map3){
                $query->whereOr([$map1, $map2, $map3]);
            })->where([])->group('cid')->order('study_count desc')->limit(10)->select();

        foreach($hot as $kk=>$vv){
            $min = \app\wap\model\Video::where(['cid' => $vv['cid']]) -> order('sort')->find();
            $playing = UserCourse::where(['cid' => $vv['cid'], 'status' => 0])->value('count(cid)');

            if ($vv['id'] != $min['id']) {
                $min['count'] = $vv['count'];
                $min['playing'] = $playing;
                $hot[$kk] = $min;
            } else {
                $hot[$kk]['playing'] = $playing;
            }
            $hot[$kk]['cname'] = VideoType::where(['id' => $vv['cid']])->value('typename');

        }
        $return  = [
            'user' => $user?$user:'',
            'month_study' => $moth_study,
            'mission' => $mission,
            'type' => $type,
            'hot' => $hot
        ];
        jsonReturn(1,'访问成功',$return);
    }

    /**
     * 首页完成的数量
     */
    public function complete_count($id){
        //已完成数量
        $complete = CourseComplete::where(['status'=>1,'uid'=>$this->user_id])->count();
        //未完成数量
        $uncomplete = CourseComplete::where(['status' => 0, 'uid' => $this->user_id])->count();
        return ['complete'=>$complete,'uncomplete'=>$uncomplete];
    }

    public function getMonthCompleteCount(){
        $count = CourseComplete::where(['status'=>1,'uid'=>$this->user_id])->whereTime("complete_time","month")->count();

        jsonReturn(1,"获取成功",$count);
    }

    /**
     * 获取部门
     * @param $id
     */
    public function get_department($id){
//        $department = Db::name('department') -> where(['id'=>$id]) -> find();
        $department = Db::name('department') -> alias('a')
            -> join('user_dep b','a.id=b.dep_id')
            -> join('user c','b.user_id=c.id')
            -> where(['c.id'=>$id]) -> value('a.name');
//        dump($department);die;
        return $department;
    }

    /**
     * @return array
     */
    public function get_moth_study_count(){
        $map1 = [['status','=',0]];
        $map2 = [['complete_time','>= time',date('Y-m').'-01 00:00:00']];
        $month_study_count = UserCourse::where(function($query) use($map1,$map2){
            $query->whereOr([$map1,$map2]);
        })->group('uid')->count();
        return $month_study_count;
    }


    public function get_list($id,$page=1){
        $limit = 10;
        $next = $page+1;
        $type = VideoType::where(['status'=>1,'id'=>$id]) -> find();
        if($type){
            $type_to_user = TypeToUser::group('cid')->column('cid');
            $type_to_user = implode(',', $type_to_user);
            $map1 = [['video.cid', 'not in', $type_to_user], ['videoType.status', '=', '1']];

            $type_to_user2 = TypeToUser::where(['uid'=>$this->user_id])->group('cid')->column('cid');
            $type_to_user2 = implode(',', $type_to_user2);
            $map2 = [['video.cid', 'in', $type_to_user2],['tid','=',$type['id']], ['videoType.status', '=', '1']];

            $user = \app\api\model\User::with(['userDep'=>function($query){
                $query->withJoin(['department'=>function($query){
                    $query->where([]);
                }])->where([]);
            }])->get($this->user_id);
            $type_to_user3 = TypeToUser::whereIn('gid',array_column($user->user_dep->toArray(),'id'))->group('cid')->column('cid');
            $type_to_user3 = implode(',', $type_to_user3);
            $map3 = [['video.cid', 'in', $type_to_user3], ['videoType.status', '=', '1']];

            $detail = \app\wap\model\Video::useGlobalScope(false)
                ->field('*,count(video.cid) as count')
                ->withJoin(['videoType'=>function($query){
                    $query->where([]);
                }])->where(function($query) use($map1,$map2,$map3){
                    $query->whereOr([$map1, $map2, $map3]);
                })->where(['tid'=>$id])->group('cid')->limit(10)->select();

            foreach($detail as $kk=>$vv){
                $min = \app\wap\model\Video::where(['cid' => $vv['cid']]) -> order('sort')->find();
                $playing = UserCourse::where(['cid' => $vv['cid'], 'status' => 0])->value('count(cid)');

                if ($vv['id'] != $min['id']) {
                    $min['count'] = $vv['count'];
                    $min['playing'] = $playing;
                    $detail[$kk] = $min;
                } else {
                    $detail[$kk]['playing'] = $playing;
                }
                $detail[$kk]['cname'] = VideoType::where(['id' => $vv['cid']])->value('typename');
            }

            if(count($detail)<$limit){
                $load = 2;
            }else{
                $load = 0;
            }
            jsonReturn(1,'成功获取',['list'=>$detail,'type'=>$type['typename'],'loading'=>$load,'page'=>$next]);
        }else{
            jsonReturn(-1,'该类型不存在');
        }


    }


    /**
     * 通过文件名称获取当前视频的信息
     */
    public function current_info(){
        if(empty(input('post.id'))){
            jsonReturn(1,'无该视频',['status'=>0]);
        }
//        $res = \app\wap\model\Video::field('xc_video.*') -> where(['xc_video.id'=>input('post.id'),'b.status'=>1]) -> join('video_type b','b.id=xc_video.cid','left') -> find();
        $res = \app\api\model\Video::where(['id'=>input('id')])->find();
        if(!$res){
            jsonReturn(1,'无该视频',['status'=>0]);
        }

//        $flag = TypeToUser::where(['cid'=>$res['cid']]) -> find();
//        if($flag){
//            $flag2 = TypeToUser::where(['uid'=>$this->user_id,'cid'=>$res['cid']]) -> find();
//            if(!$flag2){
//                jsonReturn(2,'不是目标用户，不能观看');
//            }
//
//        }

        //获取当前用户当前课程的基本信息
        $is_add = UserCourse::where(['cid'=>$res['cid'],'uid'=>$this->user_id]) -> find();

        $button_type = 1;
        //如果课程存在
        if($is_add){
            //显示可加入
//            $this -> assign('is_add',1);
            $button_type = 2;
            //继续查当前课程中当前所看文件是否完成
            $complete = CourseComplete::where(['uid'=>$this->user_id,'vid'=>$res['id'],'status'=>1]) -> find();
            //如果完成则在前台显示完成
            if($complete){
                if($complete['comment_count']==0){
                    $button_type = 3;
                }else{
                    $button_type = 4;
                }

            }
        }

        $res['vcontent'] = htmlspecialchars_decode($res['vcontent']);
        //设置img元素的最大宽度为100%
        $res['vcontent'] = str_replace('<img','<img style="max-width:100%"',$res['vcontent']);

        $res['vcontent'] = preg_replace("/(<a .*?href=\")((?!http).*?)(\".*?>)/is", "\${1}".SITE_URL."\${2}\${3}", $res['vcontent']);
        $res['vcontent'] = preg_replace("/(<img .*?src=\")((?!http).*?)(\".*?>)/is", "\${1}".SITE_URL."\${2}\${3}", $res['vcontent']);
        $res['vcontent'] = preg_replace("/(<video .*?src=\")((?!http).*?)(\".*?>)/is", "\${1}".SITE_URL."\${2}\${3}", $res['vcontent']);

        if($res){
            jsonReturn(1,'访问成功',['data'=>$res,'status'=>1,'button'=>$button_type]);
        }else{
            jsonReturn(1,'无该视频',['data'=>$res,'status'=>0]);
        }

    }

    /**
     * 视频详情页中的视频列表
     */
    public function like_video($id){
        //获取当前文件所属分类的信息
        $cid = \app\api\model\Video::where(['id'=>$id]) -> value('cid');
        $list = \app\wap\model\Video::where(['cid'=>$cid]) -> order('sort') -> select();
        jsonReturn(1,'访问成功',$list);
    }


    /**
     * 获取当前视频的评论列表
     */
    public function get_comment($vid,$page=1){
        $totalPage = 10;

        $next = $page+1;
        //获取该文件所有心得体会
        $acquaintance = Db::name('acquaintance') -> field('a.pic,a.id,a.vid,a.title,a.content,b.head_pic,b.nickname,a.time') -> alias('a') -> join('user b','a.uid=b.id') -> where(['a.vid'=>$vid]) -> order('a.time desc') -> page($page,$totalPage) -> select();
        $site = SITE_URL;

        foreach($acquaintance as $k=>$v){
            if(stripos($v['head_pic'],'http')===false){
                $acquaintance[$k]['head_pic'] = $site.$v['head_pic'];
            }
//            $acquaintance[$k]['head_pic'] = $site.$v['head_pic'];
            $pic = Db::name('upload_pic') -> whereIn('id',$v['pic']) -> limit(4) -> select();
            foreach($pic as $kk=>$vv){
                $pic[$kk]['path'] = "{$site}{$vv['path']}";
            }
            $acquaintance[$k]['file'] = $pic;
        }
        if(count($acquaintance)<$totalPage){
            $loading = 2;
        }else{
            $loading = 0;
        }

        jsonReturn(1,'',['comment'=>$acquaintance,'loading'=>$loading,'p'=>$next]);
    }

    /**
     * 查看心得详情
     */
    public function show_ac($id){
        $acquaintance = Db::name('acquaintance') -> where(['id'=>$id]) -> find();

        $site = SITE_URL;

        $pic = Db::name('upload_pic') -> whereIn('id',$acquaintance['pic']) -> select();
        foreach($pic as $kk=>$vv){
            $pic[$kk]['path'] = "{$site}{$vv['path']}";
        }
        $acquaintance['file'] = $pic;

        jsonReturn(1,'',$acquaintance);
    }

    /**
     * 获取我的所有课程
     */
    public function get_course($page=1,$type='all'){
        //本月完成
        $user = $this -> user;
        $user['course'] = CourseComplete::whereTime('complete_time','month') -> count();

        $uncomplete = '';
        $complete = '';
        if($type=='all'){
            //未完成的
            $uncomplete = $this -> get_uncomplete($user['id'],$page);
            //已完成的
            $complete = $this -> get_complete($user['id'],$page);
        }elseif($type=='complete'){
            $complete = $this -> get_complete($user['id'],$page);
            $uncomplete = '';
        }elseif($type=='uncomplete'){
            $uncomplete = $this -> get_uncomplete($user['id'],$page);
            $complete = '';
        }
        //获取我的学习数据
        $total = array();
        $beginToday=date('Y-m-d 00:00:00');
        $endToday=date('Y-m-d 23:59:59');
        $total['today'] = Db::name('course_complete')->where(['uid'=>$user['id']])->where('time', '>', $beginToday)->where('time', '<', $endToday)->count();
        $my_type = Db::name('course_complete')->alias('a')->join('__VIDEO__ b','a.vid = b.id')->where(['a.uid'=>$user['id']])->column('tid');
        $total['type'] = count(array_unique($my_type));
        $total['all'] = Db::name('course_complete')->where(['uid'=>$user['id']])->count();
        jsonReturn(1,'成功获取',['user'=>$user,'complete'=>$complete,'uncomplete'=>$uncomplete,'total'=>$total]);
    }


    /**
     * 获取未完成的课程
     * @param $uid
     * @param int $page
     * @return array
     */
    public function get_uncomplete($uid,$page=1){
        $limit = 10;

        $list = CourseComplete::with('video',function($query){
            $query ->order('cid,sort');
        })->where(['status'=>0,'uid'=>$this->user_id])->page($page,$limit)->select();

        if(count($list)<$limit){
            $loading = 2;
        }else{
            $loading = 0;
        }
        return ['loading'=>$loading,'list'=>$list,'page'=>$page+1];
    }

    /**
     * 获取已完成的课程
     * @param $uid
     * @param int $page
     * @return array
     */
    public function get_complete($uid,$page=1){
        $limit = 10;
        $list = Db::name('course_complete') -> alias('a') -> leftjoin('xc_video b','a.vid=b.id') -> leftjoin('xc_video_type c','b.tid=c.id') -> where(['a.uid'=>$uid,'a.status'=>1]) -> field('a.*, b.*, c.typename') -> order('b.cid,b.sort') -> page($page,$limit) -> select();
//        $list = CourseComplete::with('video',function($query){
//            $query -> order('cid,sort');
//        })->where(['uid'=>$this->user_id,'status'=>1])->page($page,$limit)->select();
        $site = SITE_URL;
        if(!empty($list)){
            foreach ($list as $k=>$v){
                $list[$k]['vimg'] = "{$site}{$v['vimg']}";
                $list[$k]['time'] = date('Y-m-d', strtotime($list[$k]['time']));
                $list[$k]['complete_time'] = date('Y-m-d', strtotime($list[$k]['complete_time']));
            }
        }
        if(count($list)<$limit){
            $loading = 2;
        }else{
            $loading = 0;
        }
        return ['loading'=>$loading,'list'=>$list,'page'=>$page+1];
    }


    /**
     * 处理当前课程（加入或完成等）
     * @param $type
     * @param $vid
     */
    public function course_handle($type,$vid){
        $cid = \app\api\model\Video::where(['id'=>$vid]) -> value('cid');
        if($type==1){
            $this -> add_course($vid,$cid);
        }elseif($type==2){
            $this -> complete_course($vid,$cid);
        }
    }

    /**
     * 加入我的课程
     * @param $vid
     * @param $cid
     */
    public function add_course($vid,$cid){
        $is_add = UserCourse::where(['cid' => $cid, 'uid' => $this->user_id])->find();
        if ($is_add) {
            jsonReturn(2, '课程中已存在');
        }

        Db::startTrans();
        $res = UserCourse::create(['vid' => $vid, 'cid' => $cid, 'uid' => $this->user_id, 'time' => date('Y-m-d-H:i:s')]);
        $field = $this->user_id . " as uid,id as vid,cid,CURRENT_TIMESTAMP as time,duration";
        $add = \app\api\model\Video::field($field)->where(['cid' => $cid])->select()->toArray();
        $res2 = (new CourseComplete)->saveAll($add);
        $res3 = \app\api\model\Video::where(['cid' => $cid])->setInc('study_count');

        if ($res && $res2 && $res3) {
            Db::commit();
            jsonReturn(1, '成功加入课程');
        } else {
            Db::rollback();
            jsonReturn(0, '加入失败');
        }

    }


    /**
     * 获取完成的课程信息
     * @param $vid
     * @param $cid
     */
    public function complete_course($vid,$cid){
        Db::startTrans();
        //将课程状态改变
        $res = CourseComplete::where(['uid' => $this->user_id, 'vid' => $vid, 'cid' => $cid])
            ->update(['complete_time' => date('Y-m-d H:i:s'), 'status' => 1]);
        //获取我在本课程整个课程完成的数量
        $count = CourseComplete::where(['uid' => $this->user_id, 'cid' => $cid, 'status' => 1])->count();
        //获取整个课程的数量
        $count2 = \app\api\model\Video::where(['cid' => $cid])->count();
        //相等说明整个课程完成，并改变状态
        if ($count == $count2) {
            UserCourse::where(['cid' => $cid,'uid'=>$this->user_id])->update(['status' => 1, 'complete_time' => date('Y-m-d H:i:s')]);
        }

        //更改积分
        $integral = \app\wap\model\Video::where(['id' => $vid])->value('integral');

        $res3 = \app\common\model\User::savePoint($this->user_id,$integral,"完成课程（VID：{$vid}）加了$integral", 3);

        $res5 = Integral::create(['vid'=>$vid,'uid'=>$this->user_id,'integral'=>$integral,'time'=>date('Y-m-d H:i:s')]);
        if($res && $res3!==false  && $res5){
            Db::commit();
            jsonReturn(1, "完成该课程,+$integral 分");
        } else {
            Db::rollback();
            jsonReturn(0, '提交失败');
        }
    }



    /**
     * 心得图片上传
     */
    public function ac_upload_pic($id_arr=''){
        $files = request()->file();
        $path = [];
        if ($files) {
            foreach ($files as $k => $file) {
                // 移动到框架应用根目录/uploads/ 目录下
                $info = $file->validate(['ext' => 'jpg,png,gif,jpeg'])->move('./uploads/acquaintance');
                if ($info) {
                    $file_path = trim($info->getPathName(), '.');
                    $file_path = str_replace("\\", "/", $file_path);
                    $ext = $info->getExtension();
                    $time = date('Y-m-d H:i:s');
                    $id = Db::name('upload_pic')->insertGetId([
                        'uid' => $this->user_id,
                        'path' => $file_path,
                        'ext' => $ext,
                        'add_time' => $time
                    ]);
                    $id_arr = $id_arr . $id . ',';

                    $path[] = ['id' => $id, 'path' => SITE_URL.$file_path];
                } else {
                    jsonReturn('0', $file->getError());
                    die;
                }
            }
            jsonReturn(1, '上传成功', ['id_arr' => $id_arr, 'path' => $path]);
        } else {
            jsonReturn(0, '无效图片');
        }
    }


    /**
     * 添加心得
     */
    public function add_acquaintance(){
        Db::startTrans();
        $post = input('post.');
        $post['uid'] = $this->user_id;
        $post['time'] = date('Y-m-d H:i:s');
        $post['pic'] = trim($post['pic'], ',');
        unset($post['token']);
        $res = Acquaintance::create($post);

        $res2 = CourseComplete::where(['vid' => $post['vid'], 'uid' => $this->user_id, 'status' => 1])->setInc('comment_count',1);

        $res3 = CourseComplete::where(['vid' => $post['vid'], 'uid' => $this->user_id, 'status' => 1])->update(['comment_time' => date('Y-m-d H:i:s')]);
        if ($res) {
            if ($res2 && $res3) {
                Db::commit();
                jsonReturn(1, '发布成功');
            } else {
                Db::rollback();
                jsonReturn(0,'未完成该课程，请先完成该课程');
            }
        } else {
            Db::rollback();
            jsonReturn(0, '发布失败');
        }
    }






















    /**
     * 获取当前视频评论更多评论
     */
    public function get_more_comment($vid,$page){
//        $totalPage = 10;
//        $acquaintance = Db::name('acquaintance') -> field('a.pic,a.id,a.vid,a.title,a.content,b.head_pic,b.nickname,a.time') -> alias('a') -> join('user b','a.uid=b.id') -> where(['a.vid'=>$vid]) -> order('a.time desc') -> page($page,$totalPage) -> select();
//        $site = "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}";
//        foreach($acquaintance as $k=>$v){
//            $pic = Db::name('upload_pic') -> whereIn('id',$v['pic']) -> limit(4) -> select();
//            foreach($pic as $kk=>$vv){
//                $pic[$kk]['path'] = "{$site}{$vv['path']}";
//            }
//            $acquaintance[$k]['file'] = $pic;
//        }
//        if(count($acquaintance)<$totalPage){
//            $loading = 2;
//        }else{
//            $loading = 0;
//        }
//        jsonReturn(1,'',['data'=>$acquaintance,'p'=>$page+1]);
    }

    /**
     * 发送评论
     *
     */
    public function sendcomment(){
        $user = Db::name('dyuser') -> where(['id'=>4]) -> find();
        $data = [
            'uid'=>$user['id'],
            'vid'=>input('post.vid'),
            'content'=>input('post.content'),
            'time'=>date('Y-m-d H:i:s'),
            'reply_num'=>0,
            'reply_id'=>0
        ];
        $res = Db::name('video_comment') -> data($data) -> insert();
        $data['nickname'] = $user['nickname'];
        if($res){
            jsonReturn(1,'发送成功',['status'=>1,'data'=>$data]);
        }else{
            jsonReturn(1,'发送失败',['status'=>0]);
        }
    }





    /**
     * 单条评论内的发送回复
     */
    public function send_reply(){
        $user = Db::name('dyuser') -> where(['id'=>4]) -> find();
        $to = Db::name('video_comment') -> field('xc_video_comment.*,nickname') -> join('xc_dyuser','xc_video_comment.uid=xc_dyuser.id') -> where(['xc_video_comment.id'=>input('post.reply_comment')]) -> find();
        $data = [
            'uid'=>$user['id'],
            'vid'=>input('post.vid'),
            'content'=>input('post.content'),
            'time'=>date('Y-m-d H:i:s'),
            'reply_num'=>0,
            'reply_id'=>input('post.replyid'),
            'to_id'=>input('post.toid')?input('post.toid'):$to['uid'],
            'reply_comment'=>input('post.reply_comment')
        ];
        $res = Db::name('video_comment') -> data($data) -> insert();
        $data['nickname'] = $user['nickname'];
        $data['to'] = $to['nickname'];
        if($res){
            Db::name('video_comment') -> where(['id'=>input('post.replyid')]) -> setInc('reply_num',1);
            Db::name('video_comment') -> where(['id'=>input('post.reply_comment')]) -> setInc('reply_num',1);
            jsonReturn(1,'发送成功',['status'=>1,'msg'=>'发送成功','data'=>$data]);
        }else{
            jsonReturn(1,'发送失败',['msg'=>'发送成功']);
        }
    }

    /**
     * 发送弹幕
     */
    public function send_danmu(){
        $user = Db::name('dyuser') -> where(['id'=>4]) -> find();
//        if(!$user)
//        {
//            $this -> returndata(0,'请先登录');
//        }
        $data = [
            'text'=>input('post.text'),
            'color'=>input('post.color'),
            'time'=>input('post.time'),
            'uid'=>$user['id'],
            'vid'=>input('vid'),
            'sendtime'=>date('Y-m-d H:i:s')
        ];
        $res = Db::name('danmu') -> data($data) -> insert();
        if($res){
            jsonReturn(1,'发送成功',['status'=>1]);
        }else{
            jsonReturn(1,'发送失败',['status'=>0]);
        }
    }
}
